//
//  YUYAAreaListViewController.m
//  com.yigu.yuya
//
//  Created by hxp on 16/3/9.
//  Copyright © 2016年 hxp. All rights reserved.
//

#import "YAYIAreaListViewController.h"
#import "YAYIAllAreaListTableView.h"
#import "YAYIHotAreaListCell.h"
//#import "YAYIAppointhospitallController.h"



@interface YAYIAreaListViewController ()<UITableViewDataSource,UITableViewDelegate, YAYIAllAreaListTableViewDelegate>
{
    BOOL       isSelected;
    YAYIHotAreaListCell *lastCell;
}
@property (nonatomic,strong)UIButton *backBtn;
@property (nonatomic,strong)UIView *headerView;
@property (nonatomic,strong)UILabel *seprateLine;
@property (nonatomic,strong)UITableView *hotListTableView;
@property (nonatomic,strong)YAYIAllAreaListTableView *allAreaListView;
@property (nonatomic,strong)UIImageView *iconImageView;
@property (nonatomic,strong)UILabel *currentLocalLabel;
@property (nonatomic,strong)NSMutableArray *dataAry;
@end

@implementation YAYIAreaListViewController


- (void)setCurAreaName:(NSString *)curAreaName
{
    _curAreaName = curAreaName;
    
     self.currentLocalLabel.text = [NSString stringWithFormat:@"当前位置：%@", curAreaName];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    isSelected = NO;
    self.view.backgroundColor = [UIColor whiteColor];
    
    
    UILabel *titleView = [[UILabel  alloc] initWithFrame:CGRectMake(0, 0, 80, 20)];
    titleView.textColor = [UIColor  whiteColor];
    titleView.text = @"选择地区";
    titleView.textAlignment = NSTextAlignmentCenter;
    titleView.font = [UIFont  systemFontOfSize:18 weight:0];
    self.navigationItem.titleView = titleView;
    [self createBackButton];
    [self createHeaderView];
    [self createHotAreaListTableView];
    [self createAllAreaListTableView];
    [self createDataSource];
    
    
//    self.hotListTableView.clearsSelectionOnViewWillAppear = NO;
    
}

-(void)viewWillAppear:(BOOL)animated
{
    
    [super viewWillAppear:YES];
    
}

-(void)viewDidAppear:(BOOL)animated
{
    
    [super viewDidAppear:animated];
//    // 默认选中第一行
//    [self.hotListTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
//    
//    [self tableView:self.hotListTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
    
}


/** 创建取消按钮
 *
 */
-(void)createBackButton{
    if (self.backBtn == nil) {
        self.backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [self.backBtn setTitle:@"取消"  forState:UIControlStateNormal];
        self.backBtn.titleLabel.font = [UIFont  systemFontOfSize:font15];
        [self.backBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.backBtn.frame = CGRectMake(0, 0, 40, 20);
        [self.backBtn addTarget:self action:@selector(backAction:) forControlEvents:UIControlEventTouchUpInside];
        UIBarButtonItem *item = [[UIBarButtonItem  alloc] initWithCustomView:self.backBtn];
        self.navigationItem.leftBarButtonItem = item;
    }
}
-(void)createHeaderView
{
    if (self.headerView == nil) {
        self.headerView = [UIView new];
        [self.view addSubview:self.headerView];
    }
    [self.headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, 44));
    }];
    if (self.currentLocalLabel ==  nil) {
        self.currentLocalLabel = [UILabel new];
        self.currentLocalLabel.textColor = [UIColor colorWithRed:87/255.0 green:85/255.0 blue:85/255.0 alpha:1];
        
        NSString *curAreaName = @"全国";
        if (self.curAreaName) {
            curAreaName = self.curAreaName;
        }
        self.currentLocalLabel.text = [NSString stringWithFormat:@"当前位置：%@", curAreaName];
        
        
        self.currentLocalLabel.font = [UIFont systemFontOfSize:font15];
        [self.headerView addSubview:self.currentLocalLabel];
    }
    [self.currentLocalLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.headerView.mas_centerY);
        make.left.equalTo(@15);
        make.size.mas_equalTo(CGSizeMake(200, 20));
    }];
    
    if (self.seprateLine == nil) {
        self.seprateLine = [UILabel new];
        self.seprateLine.backgroundColor = YAYIColor(222, 222, 222);
        [self.view addSubview:self.seprateLine];
    }
    [self.seprateLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.headerView.mas_bottom);
        make.left.equalTo(@0);
        make.size.mas_equalTo(CGSizeMake(SCREEN_width, 0.5));
    }];
}
/**
 * 创建地区列表
 */
-(void)createHotAreaListTableView{
    if (self.hotListTableView == nil) {
        self.hotListTableView = [[UITableView alloc] init];
        self.hotListTableView.dataSource = self;
        self.hotListTableView.tag = 1001;
        self.hotListTableView.delegate = self;
        self.hotListTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.hotListTableView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        self.hotListTableView.showsVerticalScrollIndicator = NO;
        [self.view addSubview:self.hotListTableView];
    }
    [self.hotListTableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.top.mas_equalTo(self.seprateLine.mas_bottom);
        make.bottom.equalTo(self.view);
        make.width.equalTo(@(0.427*SCREEN_width));
//        make.size.mas_equalTo(CGSizeMake(0.427*SCREEN_width, SCREEN_height - 64));
    }];
}
/**
 * 创建全国列表
 */
-(void)createAllAreaListTableView
{
    self.allAreaListView = [[YAYIAllAreaListTableView alloc] initWithFrame:CGRectZero];
    self.allAreaListView.title = @"";
    self.allAreaListView.delegate = self;
    self.allAreaListView.backgroundColor = [UIColor whiteColor];
    [self.view  addSubview:self.allAreaListView];
    [self.allAreaListView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.hotListTableView.mas_right);
        make.top.mas_equalTo(self.seprateLine.mas_bottom);
        make.bottom.equalTo(self.view);
        make.width.equalTo(@((1 - 0.427)*SCREEN_width));
//        make.size.mas_equalTo(CGSizeMake((1 - 0.427) * SCREEN_width, SCREEN_height - 64));
    }];
}


#pragma mark - 获取网络数据
-(void)createDataSource
{
    // 显示hud
    [SVProgressHUD show];
    
    _dataAry = [NSMutableArray  array];
    NSDictionary *allAreaDict = @{@"id":@"000000", @"name":@"热门地区"};
    [self.dataAry addObject:allAreaDict];
    
    [YAYINetWorking   getWithURL:all_local_city_url param:nil
                          finish:^(NSData *data, NSDictionary *dict, NSError *error) {
                              if (error == nil) {
                                  if (dict == nil || [dict[@"map"] isKindOfClass:[NSNull class]] || ![dict[@"status"] isEqualToString:@"success"]) {
                                  }else{
                                      NSDictionary *map = dict[@"map"];
                                      NSArray *provinces = map[@"provinces"];
                                      for (NSDictionary *dict in provinces) {
                                          [_dataAry  addObject:dict];
                                      }
                                      [self.hotListTableView  reloadData];
                                      
                                      // 默认选中第一行
                                      [self.hotListTableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0] animated:YES scrollPosition:UITableViewScrollPositionNone];
                                      [self tableView:self.hotListTableView didSelectRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
                                  }
                              }
                              
                              // 取消显示hud
                              [SVProgressHUD dismiss];
                              
                          }];
}


-(void)backAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark UITableViewDelegate
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *indefiter = @"indefiter";
        YAYIHotAreaListCell *cell =  [tableView dequeueReusableCellWithIdentifier:indefiter];
        if (cell == nil) {
            cell = [[YAYIHotAreaListCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:indefiter];
            cell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
        }
        NSDictionary *dict = _dataAry[indexPath.row];
        [cell.selectBtn setImage:[UIImage imageNamed:@"ic_Triangle Copy"] forState:UIControlStateNormal];
        cell.local.text = dict[@"name"];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 44;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _dataAry.count;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
//    lastCell.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
//    lastCell.selectBtn.hidden = YES;
    
//    YAYIHotAreaListCell *cell = [tableView  cellForRowAtIndexPath:indexPath];
    self.allAreaListView.title = @"不限";
//    
//    if (0 == indexPath.row) {
//        return;
//    }
    
//    NSString *name = [_dataAry[indexPath.row] valueForKey:@"name"];
//    NSString *idStr = [NSString stringWithFormat:@"%@",[_dataAry[indexPath.row] valueForKey:@"id"]];
//    if (name != nil) {
//        if ([name containsString:@"区"]) {
//            NSString *cid = [NSString stringWithFormat:@"%@",[_dataAry[indexPath.row] valueForKey:@"id"]];
//            NSString *cd = [cid  substringToIndex:2];
//            [self.allAreaListView  createDataSource:cd];
//        }else if ([name  containsString:@"市"]){
//            NSString *cid = [NSString stringWithFormat:@"%@",[_dataAry[indexPath.row] valueForKey:@"id"]];
//            NSString *cd = [cid  substringToIndex:2];
//            [self.allAreaListView  createDataSource:cd];
//        }else if ([name containsString:@"省"]){
//            NSString *cid = [NSString stringWithFormat:@"%@",[_dataAry[indexPath.row] valueForKey:@"id"]];
//            NSString *cd = [cid  substringToIndex:3];
//            [self.allAreaListView  createDataSource:cd];
//        }
//        [self.allAreaListView  setMyViewController:self];
//    }
//    NSString *cd = [cid  substringToIndex:3];
    NSString *cid = [NSString stringWithFormat:@"%@",[_dataAry[indexPath.row] valueForKey:@"id"]];
    NSString *cd = [cid  substringToIndex:2];
    
    [self.allAreaListView  createDataSource:cd areaName:[_dataAry[indexPath.row] valueForKey:@"name"]];
    [self.allAreaListView  setMyViewController:self];
    
}

//-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
//{
//    return 44;
//}

//-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
//{
//    UIView *headerView = [UIView  new];
//    headerView.backgroundColor = [UIColor colorWithWhite:0.965 alpha:1];
//    self.iconImageView = [UIImageView new];
//    [self.iconImageView setImage:[UIImage imageNamed:@"ic_Triangle Copy"]];
//    [headerView addSubview:self.iconImageView];
//    [self.iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.mas_equalTo(headerView.mas_centerY);
//        make.left.equalTo(@10);
//        make.size.mas_equalTo(CGSizeMake(10, 27/2.0));
//    }];
//    UILabel *titleLabel = [UILabel new];
//    titleLabel.text = @"全国";
//    titleLabel.font = [UIFont systemFontOfSize:font16];
//    titleLabel.textColor  = [UIColor colorWithRed:87/255.0 green:85/255.0 blue:85/255.0 alpha:1];
//    [headerView addSubview:titleLabel];
//    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.mas_equalTo(self.iconImageView.mas_right).with.offset(2);
//        make.centerY.mas_equalTo(headerView.mas_centerY);
//        make.right.equalTo(@10);
//    }];
//    headerView.userInteractionEnabled = YES;
//    UILabel *seprateLine = [UILabel new];
//    seprateLine.backgroundColor = [UIColor  lightGrayColor];
//    [headerView addSubview:seprateLine];
//    [seprateLine mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.left.equalTo(@0);
//        make.right.equalTo(@0);
//        make.bottom.mas_equalTo(headerView.mas_bottom).with.offset(1);
//        make.height.equalTo(@0.5);
//    }];
//    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(localContent:)];
//    
//    [headerView addGestureRecognizer:tap];
//    return headerView;
//    
//}


#pragma mark YAYIAllAreaListTableViewDelegate代理
- (void)countryCode:(NSString *)countryCode countryName:(NSString *)countryName
{
    if ([self.delegate respondsToSelector:@selector(countryCode:countryName:)]) {
        [self.delegate countryCode:countryCode countryName:countryName];
    }
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)localContent:(UITapGestureRecognizer *)tap
{
    self.iconImageView.hidden = NO;
    lastCell.selectBtn.hidden = YES;
    self.allAreaListView.title = @"";
    //[self.dataAry removeAllObjects];
    //[self.hotListTableView  reloadData];
//     [self.allAreaListView  createDataSource:@""];
     [self.allAreaListView  setMyViewController:self];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}
//-(void)nolimitAAction:(UITapGestureRecognizer *)tap
//{
//    
//    YAYIAppointhospitallController *appView = [YAYIAppointhospitallController new];
//    [self.navigationController pushViewController:appView animated:YES];
//    
//}

@end
